<?php

namespace App\Http\Controllers\Api;

use App\Course;
use function foo\func;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class CourseController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $orderBy = $request->order_by ? $request->order_by : $this->orderBy;
        $orderType = $request->order_type ? $request->order_type : $this->orderType;
        $pageSize = $request->page_size ? $request->page_size : $this->pageSize;
        if ($request->get('random')) {
            $lists = Course::inRandomOrder();
        } else {
            $lists = Course::orderBy($orderBy, $orderType);
        }
        if ($request->category_id) {
            $lists->where('category_id', $request->category_id);
        }
        if ($request->is_on_sale) {
            $lists->where('is_on_sale', 1);
        }
        if ($request->get('user_id')) {
            $lists->where('user_id', $request->user_id);
        }
        if ($request->get('is_required')) {
            $lists->where('is_required', 1);
        }
        if ($request->get('is_best')) {
            $lists->where('is_best', 1);
        }
        if ($request->get('is_free')) {
            $lists->where('price', 0);
        }
        if ($request->get('name')) {
            $lists->where('name', 'like', '%' . $request->get('name') . '%');
        }
        if ($request->get('platform') == 'backend') {
            $user = Auth::user();
            if ($user->hasRole('Teacher') && !$user->hasRole('Admin') && !$user->hasRole('Developer')) {
                $lists->where("user_id", Auth::id());
            }
        }
        return $lists->with('user')->paginate($pageSize);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       return Course::create($request->all());
    }

    /**
     * @param Course $course
     * @return Course
     */
    public function show(Course $course)
    {
        foreach ($course->catalogs as $k => &$catalog) {
            if ($k == 0) {
                $catalog->active = true;
            } else {
                $catalog->active = false;
            }
        }
        $course->user;
        $course->pv = $course->pv + 1;
        $course->save();
        return $course;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Course  $course
     * @return \Illuminate\Http\Response
     */
    public function edit(Course $course)
    {
        return response($course);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Course  $course
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Course $course)
    {
        $course->update($request->except(['category_name', 'purchased']));
        return response($course);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Course  $course
     * @return \Illuminate\Http\Response
     */
    public function destroy(Course $course)
    {
        $user = Auth::user();
        if ($user->hasRole('Teacher') && !$user->hasRole('Admin') && !$user->hasRole('Developer')) {
            if ($course->user_id != $user->id) {
                return response(['message' => '权限不足'], 400);
            }
        }
        $course->delete();
        return response(['message' => '删除成功']);
    }

    public function group(Request $request)
    {

        $best = $this->_getCourse($request)->where('is_best', 1)->limit(3)->inRandomOrder()->get();
        $free = $this->_getCourse($request)->where('price', '<=', 0)->limit(3)->inRandomOrder()->get();
        $all  = $this->_getCourse($request)->limit(3)->inRandomOrder()->get();
        return [
            'best' => $best,
            'free' => $free,
            'all'  => $all,
        ];
    }

    protected function _getCourse(Request $request) {
        $course = Course::where('is_on_sale', 1)->whereHas('user')->with('user');;
        if ($request->category_id) {
            $course->where('category_id', $request->category_id);
        }
        return $course;
    }
}
